package com.dodayum.service;

import com.dodayum.config.MyEntityRepository;
import com.dodayum.entity.NginxLogInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @version 1.0
 * 备注：
 * @Author BaiYu
 * </br>@Date 2024/12/26 16:27
 */

@Service
public class MyEntityEasyService {

    @Resource
    private MyEntityRepository myEntityRepository;

    public Iterable<NginxLogInfo> findAll(){
        return myEntityRepository.findAll();
    }

    public NginxLogInfo findById(String id){
        Optional<NginxLogInfo> byId = myEntityRepository.findById(id);
        return byId.orElse(null);
    }

    public NginxLogInfo save(NginxLogInfo nginxLogInfo){
        return myEntityRepository.save(nginxLogInfo);
    }

    public void deleteById(String id){
        myEntityRepository.deleteById(id);
    }

    public NginxLogInfo updateById(String id, NginxLogInfo nginxLogInfo) throws Exception {
        Optional<NginxLogInfo> optionalEntity = myEntityRepository.findById(id);
        if (optionalEntity.isPresent()) {
            nginxLogInfo.setId(optionalEntity.get().getId());
            return myEntityRepository.save(nginxLogInfo);
        }else {
            throw new Exception("没有值");
        }
    }

}
